package com.nine.pattern.singleton;

import com.nine.pattern.singleton.demo1.Singleton01;
import com.nine.pattern.singleton.demo1.Singleton02;
import com.nine.pattern.singleton.demo2.Singleton03;
import com.nine.pattern.singleton.demo2.Singleton04;
import com.nine.pattern.singleton.demo2.Singleton05;
import com.nine.pattern.singleton.demo2.Singleton06;
import com.nine.pattern.singleton.demo3.Singleton07;

/**
 * @author nine
 * @version 1.0
 * @description TODO
 * @date 2024-06-25 23:52
 */
public class TestClient {

	public static void main(String[] args) {
		// test01();
		// test02();
		test03();
		test04();
		test05();
		test06();
		test07();
	}

	// 饿汉式：静态变量
	private static void test01() {
		// 创建Singleton类的对象
		Singleton01 singleton01 = Singleton01.getInstance();
		Singleton01 singleton02 = Singleton01.getInstance();
		System.out.println(singleton01 == singleton02);
	}

	// 饿汉式：静态方法
	private static void test02() {
		// 创建Singleton类的对象
		Singleton02 singleton01 = Singleton02.getInstance();
		Singleton02 singleton02 = Singleton02.getInstance();
		System.out.println(singleton01 == singleton02);
	}

	// 懒汉式：线程不安全
	private static void test03() {
		// 创建Singleton类的对象
		Singleton03 singleton01 = Singleton03.getInstance();
		Singleton03 singleton02 = Singleton03.getInstance();
		System.out.println(singleton01 == singleton02);
	}


	// 懒汉式：线程安全
	private static void test04() {
        // 创建Singleton类的对象
        Singleton04 singleton01 = Singleton04.getInstance();
		Singleton04 singleton02 = Singleton04.getInstance();
        System.out.println(singleton01 == singleton02);
    }

    // 懒汉式：双重检查
    private static void test05() {
        // 创建Singleton类的对象
	    Singleton05 singleton01 = Singleton05.getInstance();
		Singleton05 singleton02 = Singleton05.getInstance();
        System.out.println(singleton01 == singleton02);
	}

	// 懒汉式：静态内部类
	private static void test06() {
		Singleton06 singleton01 = Singleton06.getInstance();
		Singleton06 singleton02 = Singleton06.getInstance();
		System.out.println(singleton01 == singleton02);
	}

	// 饿汉式：枚举
	private static void test07() {
		Singleton07 singleton01 = Singleton07.INSTANCE;
		Singleton07 singleton02 = Singleton07.INSTANCE;
		System.out.println(singleton01 == singleton02);
	}

}
